package net.sourceforge.jweb.jstl.easyui;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.util.StringUtils;

import net.sourceforge.jweb.annotation.Attribute;
import net.sourceforge.jweb.annotation.Tag;

@Tag(name = "formReference", description = "refer extral http form element to server interactive tag")
public class RequestParameterTag extends SimpleTagSupport {
	private String fieldName;
	private String selector;
	private String type = "textbox";
	private boolean multiSelect = true;
	private String statement;

	@Override
	public void doTag() throws JspException, IOException {
		DataGridTag grid = (DataGridTag) this.getParent();
		grid.extralParams.add(this);
		super.doTag();
	}

	@Attribute(required = true, description = "your final field name when do HTPP request, blank does not support")
	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	@Attribute(required = true, description = "extral element jquery selector, just the selector, brace and dollar syntax does not included")
	public String getSelector() {
		return selector;
	}

	public void setSelector(String selector) {
		this.selector = selector;
	}

	@Attribute(required = false, description = "easyui form type representation, default is textbox, such as textbox, passwordbox, it will reflect to method call, i.e. selector.textbox(getValue);")
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Attribute(required = false, description = "use multi select or not if element is easyui multiple value UI, such as combox. if true, it will use getValues method")
	public boolean isMultiSelect() {
		return multiSelect;
	}

	public void setMultiSelect(boolean multiSelect) {
		this.multiSelect = multiSelect;
	}

	@Attribute(required = false, description = "if you are not using easyui form element, you can provide your jquery statement to getvalue. for example, $(abc).val();")
	public String getStatement() {
		return statement;
	}

	public void setStatement(String statement) {
		this.statement = statement;
	}

	String evalJqueryStatement() throws JspException {
		if (this.fieldName.trim().isEmpty()) {
			throw new JspException("field should not be null");
		}
		if (this.selector.trim().isEmpty()) {
			throw new JspException("field should not be null");
		}
		if (this.statement != null && StringUtils.hasText(this.statement)) {
			return this.statement;
		}
		StringBuilder builder = new StringBuilder(128);

		switch (this.type.trim()) {
		case "textbox":
		case "passwordtbox":
		case "maskedbox":
		case "numberbox":
		case "datebox":
		case "datetimebox":
		case "datetimespinner":
		case "calendar":
		case "spinner":
		case "timespinner":
		case "slider":
		case "filebox":
			builder.append("$('").append(this.selector.trim()).append("').");
			builder.append(this.type.trim()).append("('getValue')");
			break;
		case "combo":
		case "combobox":
		case "combotree":
		case "combogrid":
		case "combotreegrid":
		case "tagbox":
			builder.append("$('").append(this.selector.trim()).append("').");
			if (this.multiSelect) {
				builder.append(this.type.trim()).append("('getValues')");
			} else {
				builder.append(this.type.trim()).append("('getValue')");
			}
			break;
		case "radiobutton":
			builder.append("$('input[name=\"").append(this.fieldName.trim()).append("\"]:checked').val()");
			break;
		case "checkbox":
			builder.append("$('input[name=\"").append(this.fieldName.trim())
					.append("\"]:checked').map(function () {return $(this).val();}).get().join(',')");
			break;
		default:
			throw new JspException(this.type + " is unsupported");
		}

		return builder.toString();
	}
}
